import 'package:flutter/material.dart';

///ListView
///下拉刷新 RefreshIndicator
///滑动方向 scrollDirection
class ListViewDemo extends StatefulWidget {
  // const ListViewDemo({Key? key}) : super(key: key);

  @override
  _ListViewDemoState createState() => _ListViewDemoState();
}

class _ListViewDemoState extends State<ListViewDemo> {
  ///是否显示FloatingActionButton
  bool show = false;
  List<RowWidget> _list = [];

  ///滑动控制器
  late ScrollController _scrollController;

  @override
  void initState() {
    for (int i = 1; i < 100; i++) {
      _list.add(RowWidget(indexX: i));
    }
    _scrollController = new ScrollController();
    _scrollController.addListener(() {
      if (_scrollController.offset >= 100 && show == false) {
        setState(() {
          show = true;
        });
      }else if(_scrollController.offset < 100 && show == true){
        setState(() {
          show = false;
        });
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    _scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("listView")),
      floatingActionButton: show
          ? FloatingActionButton(
              onPressed: () {
                ///一秒后滑动到顶部
                _scrollController.animateTo(0, duration: Duration(microseconds: 300), curve: Curves.easeInBack);
              },
              child: Icon(Icons.add),
            )
          : null,
      body: Scrollbar(
      /*      child: RefreshIndicator(
          onRefresh: _refresh,
          child: ListView(
            // reverse: true,
            // itemExtent: 50,
            // shrinkWrap: true,
            children: _list
                .map((e) => Center(
                      child: Text(e.toString() + "个憨憨"),
                    ))
                .toList(),
          ),
        )*/
        child: ListView.builder(
          scrollDirection: Axis.vertical,
          itemBuilder: (context, index) {
            if (index == 0 || (index % 10 == 0)) {
              return Text("我是title",
                  textAlign: TextAlign.center,
                  style: TextStyle(color: Colors.blue));
            }
            return _list[index];
          },
          itemCount: _list.length,
          controller: _scrollController,
        ),
      ),
    );
  }

  ///上拉刷新
  Future _refresh() async {
    ///延时执行-函数体
    await Future.delayed(Duration(seconds: 3), () {
      print("hdjshf");
    });
    return "";
  }
}

class RowWidget extends StatelessWidget {
  final int indexX;

  const RowWidget({Key? key, required this.indexX}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Icon(Icons.add),
        Text(
          "我是数据呀 $indexX",
          textAlign: TextAlign.center,
        )
      ],
    );
  }
}
